class Bullet extends Base {
	constructor(plane) {
		super(bulletImg)
		this.plane = plane;
		this.speed = 8;
		let timer = setTimeout(_ => {
			clearTimeout(timer)
			this.init()
		})
	}
	init() {
		//初始位置
		const { dom, width, height } = this.plane;
		this.left = dom.offsetLeft + (width - this.width) / 2;
		//2为阈值，子弹出来时离飞机一段距离
		this.top = dom.offsetTop - this.height * 2;
		$(this.dom).css('left', this.left + 'px');
		$(this.dom).css('top', this.top + 'px');
		this.move()
	}
	move() {
		this.inter = setInterval(_ => {
			this.top -= this.speed;
			if (this.top < -16) {
				//移除子弹
				this.dom.remove()
				clearInterval(this.inter);
			}
			$(this.dom).css('top', this.top + 'px');

			this.checkPeng();
		}, 30)
	}
	//碰撞的判断
	checkPeng() {
		for (let di of Base.diAll) {
			this.checkBeng(di, this)
		}
	}
	/************************************
	 * 移除飞机和子弹
	 * 该方法为碰撞敌机之后才调用
	 ***********************************/
	remove(di) {
		//改变子弹的效果
		this.changeSrc('boomSrc');
		if (di.blood > 0) {
			//敌机还没有死，换成受伤的状态
			di.changeSrc('beatSrc')
		} else {
			//敌机死了，换成爆炸状态
			di.changeSrc('boomSrc')
			//找到当前敌机再敌机集合中的位置
			let index = Base.diAll.findIndex(item => item === di);
			//并且从敌机数组中移除
			Base.diAll.splice(index, 1);
			//敌机挂了，等待200毫秒的动画
			let timer = setTimeout(_ => {
				clearTimeout(timer)
				//移除敌机
				di.remove(true)
			}, 200)
		}
		//子弹无论如何都要移除
		let timer = setTimeout(_ => {
			clearTimeout(timer)
			clearInterval(this.inter)
			this.dom.remove()
			//更新积分
			const jDom = $('#box>p:nth-child(2)>span');
			//获取旧的积分并更新到新的分数
			const num = Number(jDom.text()) + 10;
			//设置新的值
			jDom.text(num)
		}, 100)
	}
}
